package com.example.controller;

import com.example.dto.DormitoryPeoDto;
import com.example.pojo.Dormitory;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.DormitoryService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dormitory")
public class DormitoryController {

    @Autowired
    private DormitoryService dormitoryService;
    @Autowired
    private UserService userService;

    /**
     * 添加宿舍
     * @param dormitory
     * @return
     */
    @PostMapping
    public Result addDormitory(@RequestBody Dormitory dormitory){
        dormitoryService.addDormitory(dormitory);
        return Result.success("添加宿舍成功");
    }

    /**
     * 更新宿舍
     * @param dormitory
     * @return
     */
    @PutMapping
    public Result updateDormitory(@RequestBody Dormitory dormitory){
        dormitoryService.updateDormitory(dormitory);
        return Result.success("更新宿舍成功");
    }

    /**
     * 获取宿舍信息
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result getDormitory(@RequestParam Integer id){
        Dormitory dormitory = dormitoryService.getDormitoryById(id);
        return Result.success(dormitory);
    }

    /**
     * 删除宿舍
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteDormitory(@RequestParam Integer id){
        dormitoryService.deleteDormitory(id);
        return Result.success("删除宿舍成功");
    }

    /**
     *宿舍列表（分页）
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result listDormitory(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam(required = false) Integer categoryId) {
        PageBean pageBean = dormitoryService.listDormitory(pageNum,pageSize,categoryId);
        return Result.success(pageBean);
    }

    /**
     * 查询宿舍列表
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Dormitory> list = dormitoryService.list();
        return Result.success(list);
    }

    /**
     * 查询该宿舍有哪些人
     */
    @GetMapping("/findPeo")
    public Result findPeo(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String dormitoryId,
                          @RequestParam String categoryName){
        PageBean pageBean = userService.findPeo(pageNum,pageSize,dormitoryId,categoryName);
        return Result.success(pageBean);
    }

    /**
     * 过滤已满宿舍，不查询这类已满宿舍(置业)
     * @return
     */
    @PutMapping("/jumpByZhiYe")
    public Result jumpDormitory(){
        List<Dormitory> list = dormitoryService.findExcludeFullByZhiYe();
        return Result.success(list);
    }
}
